<template>
  <div class="line">
    <div class="main" ref="main">
    </div>
  </div>
</template>
<script>
const echarts = require('echarts/lib/echarts') // 引入echarts
require('echarts/lib/chart/bar') // 引入柱状图
// 引入提示框和标题组件
require('echarts/lib/component/tooltip')
require('echarts/lib/component/title')
export default {
  data () {
    return {
      title: {
        text1: '图表名称',
        x: 'left',
        y: 'top',
        fontFamily: 'Courier New',
        fontSize: 20,
        color: '#000'
      },
      histogramLegend: {
        backgroundColor: '#EFEBE3',
        radius: 0,
        postColosr: '#3ea915',
        labColor: '#000000',
        labSize: 10,
        yName: '单位', // y轴单位
        barWidth: '60%' // 柱子粗度
      },
      gridding: {
        isShowX: true, // x轴分割线
        isShowY: true, // y轴分割线
        lineColorX: '#c6c5ca', // x轴分割线颜色
        lineColorY: '#c6c5ca', // y轴分割线颜色
        lineTypeX: 'solid', // x轴分割线类型 实线：'solid'  线虚线：'dashed' 点虚线'dotted'
        lineTypeY: 'solid', // 轴分割线类型 实线：'solid'  线虚线：'dashed' 点虚线'dotted'
        lineWidthX: '1', // x轴分割线粗细
        lineWidthY: '1' // y轴分割线粗细
      },
      xData: ['衬衫', '羊毛衫', '雪纺衫', '裤子', '高跟鞋', '袜子'],
      yData: [5, 2, 3, 0, 10, 20]
    }
  },
  mounted () {
    this.initEcharts()
  },
  methods: {

    getData () {
      // console.log('========================================进入histogram')
      this.xData = this.$store.state.singleFile.info.xValue
      this.yData = this.$store.state.singleFile.info.yValue
      // console.log('histogram接收到的xData数组')
      // console.log(this.xData)
      // console.log('histogram接收到的yData数组')
      // console.log(this.yData)
    },
    initEcharts () {
      // const THIS = this
      this.getData()
      const mainDiv = this.$refs.main // 找到绘制的区域,强烈推荐使用refs
      const myChart = echarts.init(mainDiv) // 初始化
      myChart.setOption({ // 官网例子
        title: {
          text: this.title.text1,
          x: this.title.x, // 标题横向位置//////////////////////////////////// 增
          y: this.title.y, // 标题纵向位置//////////////////////////////////// 增
          textStyle: {
            fontFamily: this.title.fontFamily,
            fontSize: this.title.fontSize,
            color: this.title.color
          }
        },
        backgroundColor: this.histogramLegend.backgroundColor,
        // 提示框取默认值，即鼠标移动到柱状图会显示内容
        tooltip: {
          trigger: 'axis', // 触发类型；轴触发，axis则鼠标hover到一条柱状图显示全部数据，item则鼠标hover到折线点显示相应数据，
          axisPointer: { // 坐标轴指示器，坐标轴触发有效，
            type: 'shadow', // 默认为line，line直线，cross十字准星，shadow阴影
            crossStyle: {
              color: '#a9121a'
            }
          }
        },
        // 右上角工具条
        toolbox: {
          show: true,
          feature: {
            mark: {
              show: true
            },
            /* dataView: {
              show: true,
              readOnly: false
            }, */
            saveAsImage: {
              show: true
            }
          }
        },
        xAxis: {
          data: this.xData,
          axisLabel: {
            color: this.histogramLegend.labColor, // 刻度标签
            fontSize: this.histogramLegend.labSize
          },
          splitLine: {
            show: this.gridding.isShowX, // 是否显示分割线
            lineStyle: {
              color: this.gridding.lineColorX, // 分割线颜色
              type: this.gridding.lineTypeX, // 分割线类型 实线：'solid'  线虚线：'dashed' 点虚线'dotted'
              width: this.gridding.lineWidthX // 分割线粗细  1--4
            }
          }

        },
        yAxis: {
          type: 'value',
          name: this.histogramLegend.yName, // y轴单位
          axisLabel: {
            color: this.histogramLegend.labColor, // 刻度标签
            fontSize: this.histogramLegend.labSize
          },
          splitLine: {
            show: this.gridding.isShowY, // 是否显示分割线
            lineStyle: {
              color: this.gridding.lineColorY, // 分割线颜色
              type: this.gridding.lineTypeY, // 分割线类型 实线：'solid'  线虚线：'dashed' 点虚线'dotted'
              width: this.gridding.lineWidthY // 分割线粗细  1--4
            }
          }
        },
        series: [{
          // name: '待写',
          type: 'bar',
          data: this.yData,
          barWidth: this.histogramLegend.barWidth, // 柱子的粗度
          itemStyle: {
            barBorderRadius: this.histogramLegend.radius,
            color: this.histogramLegend.postColosr
          }
        }]
      })
    }
  },
  watch: {
    '$store.state.singleFile.date': function (val) {
      console.log('进入histogram===============================')
      // this.xData = this.$store.state.singleFile.info.xValue
      // this.yData = this.$store.state.singleFile.info.yValue
      this.initEcharts()
    },
    '$store.state.chartTitle.text': function (val) {
      this.title.text1 = val
      console.log('histogram接收到的title' + this.title.text)
      this.initEcharts()
    },
    '$store.state.chartTitle.titleAcross': function (val) {
      this.title.x = val
      console.log('柱状图接收到的标题横向位置' + val)
      this.initEcharts()
    },
    '$store.state.chartTitle.titleVertical': function (val) {
      this.title.y = val
      console.log('柱状图接收到的标题竖向位置' + val)
      this.initEcharts()
    },
    '$store.state.chartTitle.wordFamily': function (val) {
      this.title.fontFamily = val
      this.initEcharts()
    },
    '$store.state.chartTitle.wordSize': function (val) {
      this.title.fontSize = val
      this.initEcharts()
    },
    '$store.state.chartTitle.titleColor': function (val) {
      this.title.color = val
      this.initEcharts()
    },
    '$store.state.histogramLegend.backgroundColor': function (val) {
      this.histogramLegend.backgroundColor = val
      this.initEcharts()
    },
    '$store.state.histogramLegend.postColor': function (val) {
      this.histogramLegend.postColosr = val
      this.initEcharts()
    },

    '$store.state.histogramLegend.postRadius': function (val) {
      this.histogramLegend.radius = val
      this.initEcharts()
    },
    '$store.state.histogramLegend.labColor': function (val) {
      this.histogramLegend.labColor = val
      this.initEcharts()
    },
    '$store.state.histogramLegend.labSize': function (val) {
      this.histogramLegend.labSize = val
      this.initEcharts()
    },
    '$store.state.histogramLegend.yName': function (val) {
      this.histogramLegend.yName = val
      this.initEcharts()
    },
    '$store.state.histogramLegend.barWidth': function (val) {
      this.histogramLegend.barWidth = val + '%'
      this.initEcharts()
    },
    '$store.state.grid.isShowX': function (val) {
      console.log('柱状图接收到的是否显示x轴分割线' + val)
      this.gridding.isShowX = val
      this.initEcharts()
    },
    '$store.state.grid.isShowY': function (val) {
      this.gridding.isShowY = val
      console.log('柱状图接收到的是否显示y轴分割线' + val)
      this.initEcharts()
    },
    '$store.state.grid.lineColorX': function (val) {
      console.log('柱状图接收到X轴分割线颜色' + val)
      this.gridding.lineColorX = val
      this.initEcharts()
    },
    '$store.state.grid.lineColorY': function (val) {
      console.log('柱状图接收到Y轴分割线颜色' + val)
      this.gridding.lineColorY = val
      this.initEcharts()
    },
    '$store.state.grid.lineTypeX': function (val) {
      this.gridding.lineTypeX = val
      this.initEcharts()
    },
    '$store.state.grid.lineTypeY': function (val) {
      this.gridding.lineTypeY = val
      this.initEcharts()
    },
    '$store.state.grid.lineWidthX': function (val) {
      console.log('柱状图接收到X轴分割线粗细为' + val)
      this.gridding.lineWidthX = val
      this.initEcharts()
    },
    '$store.state.grid.lineWidthY': function (val) {
      this.gridding.lineWidthY = val
      this.initEcharts()
    }
  },
  destroyed () {
    this.$store.state.singleFile.info = null
    this.$store.state.chartTitle.id = ''
    this.$store.state.chartTitle.text = ''
    this.$store.state.chartTitle.titleAcross = ''
    this.$store.state.chartTitle.titleVertical = ''
    this.$store.state.chartTitle.wordFamily = ''
    this.$store.state.chartTitle.wordSize = ''
    this.$store.state.chartTitle.titleColor = ''

    this.$store.state.grid.isShowX = true
    this.$store.state.grid.isShowY = true
    this.$store.state.grid.lineColorX = '#c6c5ca'
    this.$store.state.grid.lineColorY = '#c6c5ca'
    this.$store.state.grid.lineTypeX = 'solid'
    this.$store.state.grid.lineTypeY = 'solid'
    this.$store.state.grid.lineWidthX = '1'
    this.$store.state.grid.lineWidthY = '1'
  }
}
</script>
<style lang="scss" scoped>
  .line{
    .main {
      width: 735px;
      height:500px;
    }
  }
</style>
